import java.util.ArrayList;

import processing.core.PApplet;


public class CheckoutView extends View implements RectangularButtonDelegate, ListViewDelegate{
	private final static int VIEW_HEIGHT = 700;
	private final static int VIEW_WIDTH = 1300;
	
	private final static int STD_OFFSET = 15;
	
	private final static int LIST_X = 300;
	private final static int LIST_Y = 0;
	private final static int LIST_ROWS = 12;
	private final static int LIST_COLS = 3;
	private final static int LIST_COLWIDTH = 180;
	
	private final static int TXT_WIDTH = 100;
	private final static int TXT_HEIGHT = 20;
	private final static int BTN_WIDTH = 100;
	private final static int BTN_HEIGHT = 40;
	
	private final static int TXTPRODUCT_X = LIST_X - TXT_WIDTH - STD_OFFSET;
	private final static int TXTPRODUCT_Y = LIST_Y + STD_OFFSET;

	private final static int TXTQUANTITY_X = TXTPRODUCT_X;
	private final static int TXTQUANTITY_Y = TXTPRODUCT_Y + TXT_HEIGHT+STD_OFFSET;
	
	private final static int BTNADD_X = LIST_X - BTN_WIDTH - STD_OFFSET;
	private final static int BTNADD_Y = TXTQUANTITY_Y + TXT_HEIGHT + 3*STD_OFFSET;
	private final static int BTNADD_ID = 0;
	
	private final static int BTNREMOVE_X = BTNADD_X;
	private final static int BTNREMOVE_Y = BTNADD_Y + BTN_HEIGHT + STD_OFFSET;
	private final static int BTNREMOVE_ID = 1;
	
	private final static int BTNDONE_X = LIST_X + LIST_COLWIDTH*LIST_COLS + 2*ListView.ROW_OFFSET + 2*STD_OFFSET;
	private final static int BTNDONE_Y = LIST_Y + (LIST_ROWS+1)*(ListView.ROW_HEIGHT + ListView.ROW_OFFSET) - BTN_HEIGHT;
	private final static int BTNDONE_ID = 2;
	
	private final static int BTNCANCEL_X = BTNDONE_X + BTN_WIDTH + 3*STD_OFFSET;
	private final static int BTNCANCEL_Y = BTNDONE_Y;
	private final static int BTNCANCEL_ID = 3;
	
	private ListView list;
	private TextBox txtProductId;
	private TextBox txtQuantity;
	private RectangularButton btnAdd;
	private RectangularButton btnRemove;
	private RectangularButton btnDone;
	private RectangularButton btnCancel;
	private LocalDBInterface localDB;
	private float total;
	
	
	/*=============================================================================================
	 * Constructor
	 *-------------------------------------------------------------------------------------------*/
	CheckoutView(float x_, float y_, MainView p) {
		super(x_, y_, VIEW_WIDTH, VIEW_HEIGHT, p);
		
		total = 0;
		list = new ListView(LIST_X, LIST_Y, LIST_COLWIDTH, LIST_ROWS, LIST_COLS, p);
		list.setDelegate(this);
		list.setTitle(0, "Product Name");
		list.setTitle(1, "Quantity");
		list.setTitle(2, "Price");
		/*localDB = null;*/ LocalDBTestStub.populateDB(); localDB = new LocalDBTestStub();
		
		txtProductId = new TextBox(TXTPRODUCT_X, TXTPRODUCT_Y, TXT_WIDTH, TXT_HEIGHT, p, "Product ID");
		txtQuantity = new TextBox(TXTQUANTITY_X, TXTQUANTITY_Y, TXT_WIDTH, TXT_HEIGHT, p, "Quantity");
		btnAdd = new RectangularButton(BTNADD_X, BTNADD_Y, BTN_WIDTH, BTN_HEIGHT, p, BTNADD_ID);
		btnRemove = new RectangularButton(BTNREMOVE_X, BTNREMOVE_Y, BTN_WIDTH, BTN_HEIGHT, p, BTNREMOVE_ID);
		btnDone = new RectangularButton(BTNDONE_X, BTNDONE_Y, BTN_WIDTH, BTN_HEIGHT, p, BTNDONE_ID);
		btnCancel = new RectangularButton(BTNCANCEL_X, BTNCANCEL_Y, BTN_WIDTH, BTN_HEIGHT, p, BTNCANCEL_ID);
		
		btnAdd.setCaption("Add"); btnAdd.setDelegate(this);
		btnRemove.setCaption("Remove"); btnRemove.setDelegate(this);
		btnDone.setCaption("Done"); btnDone.setDelegate(this);
		btnCancel.setCaption("Cancel"); btnCancel.setDelegate(this);
		btnRemove.disable();
		
		subviews.add(list);
		subviews.add(txtProductId);
		subviews.add(txtQuantity);
		subviews.add(btnAdd);
		subviews.add(btnRemove);
		subviews.add(btnDone);
		subviews.add(btnCancel);
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	void drawContent()
	{
		int listViewHeight = (LIST_ROWS+1)*(ListView.ROW_HEIGHT + ListView.ROW_OFFSET) + ListView.ROW_OFFSET;
		
		parent.fill(0,191,255);
		parent.noStroke();
		parent.rect(0,0,VIEW_WIDTH, VIEW_HEIGHT);
		
		String text = String.format("Total = %.2f", total);
		parent.textSize(15);
		parent.fill(0);
		parent.textAlign(PApplet.LEFT, PApplet.TOP);
		parent.text(text, LIST_X+(LIST_COLS-1)*LIST_COLWIDTH, LIST_Y + listViewHeight + 2*STD_OFFSET);
		
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	boolean mousePressed(float px, float py)
	{
		txtProductId.setActivated(false);
		txtQuantity.setActivated(false);
		return super.mousePressed(px, py);
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	void keyPressed( char key)
	{
		
		if( key==10)
			RectangularButtonPressed(BTNADD_ID);
		else if( key==9)
		{
			if( txtProductId.getActivated())
			{
				txtProductId.setActivated(false);
				txtQuantity.setActivated(true);
			}
			else if( txtQuantity.getActivated())
			{
				txtQuantity.setActivated(false);
				txtProductId.setActivated(true);
			}
			else
				super.keyPressed(key);
		}
		else
			super.keyPressed(key);
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	void setLocalDB( LocalDBInterface e) { localDB = e;	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public void RectangularButtonPressed(int id) {
		switch( id)
		{
		case BTNADD_ID:
			addPressed(); break;
		case BTNREMOVE_ID:
			removePressed(); break;
		case BTNDONE_ID:
			donePressed(); break;
		case BTNCANCEL_ID:
			cancelPressed(); break;
		default:
		}
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public void infoSelected() {
		btnRemove.enable();
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	public void infoUnselected() {
		btnRemove.disable();
	}

	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	private void addPressed()
	{
		ArrayList<String> newRow;
		Product product = null;
		int productId = -1;
		int quantity = -1;
		float priceItems;
		
		try	{
			productId = Integer.parseInt( txtProductId.getText());
			quantity = Integer.parseInt( txtQuantity.getText());
		} catch (NumberFormatException e) { }
		
		if( productId>=0) product = localDB.getProduct(productId);
		
		if( product != null && quantity>=0)
		{
			priceItems = product.getPrice()*quantity;
			
			newRow = new ArrayList<String>();
			newRow.add(product.getProductName());
			newRow.add(Integer.toString(quantity));
			newRow.add(String.format("%.2f",priceItems));
			
			total += priceItems;
			list.add(newRow);
		}
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	private void removePressed()
	{
		float priceItems = Float.parseFloat(list.getInfo( list.getSelectedIdx()).get(2));
		total -= priceItems;
		list.removeInfo( list.getSelectedIdx());
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	private void donePressed()
	{
		this.parent.view  = 1;
		parent.changeView();
	}
	
	/*=============================================================================================
	 * 
	 *-------------------------------------------------------------------------------------------*/
	private void cancelPressed()
	{
		this.parent.view  = 1;
		parent.changeView();
	}
	
}
